(* ==========================================================================
     Simple Streams library
     Version 1.0.4, Freeware
   --------------------------------------------------------------------------
     (c) 2012, TSX Software
   --------------------------------------------------------------------------
     Provides access to binary streams with ActiveX IStream compatible
     interface.
     Supports:
       - Files, devices and slots (see CreateFile API reference)
       - In-memory streams (library allocator)
       - Streams linked to custom memory block (read-only for now)
       - Resource streams
       - Custom streams with user-provided callbacks

     For IStream usage look at IStream MSDN reference at
     http://msdn.microsoft.com/en-us/library/windows/desktop/aa380034(v=vs.85).aspx

     Notes.

     Library can be compiled in two modes: interface reference-counted (iRC)
     and non-reference-counted (nRC).
     In iRC version you can free stream objects by releasing stream interface
     (COM-styled behaviour). As for me, I prefer nRC behaviour of interfaces.
     In nRC version you must free stream object by call to StreamFree()
     method. Do not be confused passing real ActiveX IStream interface to
     StreamFree. It will detect usage of library IStream interface, and ignore
     AX IStream-s.

     Stat() interface method is supported only in streams created with
     StreamCreateFile and StreamOpenFile methods.

     Commit(), Revert(), Clone(), LockRegion() and UnlockRegion() methods are
     unsupported in all streams.

     Custom user streams can be R+, W+ or RW+. Access mode depends on
     assigned callbacks in USTRM_PROCS structure passed to StreamCreateUser
     function.
   ========================================================================== *)

unit uLSLibImp;

interface

uses Windows, ActiveX;

const
  STRM_ANSI    = 0;
  STRM_UNICODE = 1;

  IID_IStream: TGUID = '{0000000C-0000-0000-C000-000000000046}';

type
  HLSTREAM = pointer;
  
  STRMCLOSEPROC = procedure(User: pointer); stdcall;
  STRMSIZEPROC = procedure(out StrmSize: Largeint; User: pointer); stdcall;
  STRMIOPROC = function(Data: pointer; const DataSize: Largeint; out cbAffect: Largeint; User: pointer): HResult; stdcall;
  STRMSEEKPROC = function(const Delta: Largeint; const dwOrigin: Longint; out libNewPosition: Largeint; User: pointer): HResult; stdcall;

  USTRM_PROCS = packed record
    ppClose: STRMCLOSEPROC;
    ppSize: STRMSIZEPROC;
    ppRead,
    ppWrite: STRMIOPROC;
    ppSeek: STRMSEEKPROC;
  end;

function lsStreamVersion(): PAnsiChar; stdcall;

function lsStreamCreateFile(const FileName: pointer; const Flags: cardinal; const ShareRights: cardinal; out Stream: HLSTREAM): HResult; stdcall;
function lsStreamOpenFile(const FileName: pointer; const Flags: cardinal; const AccessRights,ShareRights: cardinal; out Stream: HLSTREAM): HResult; stdcall;
function lsStreamCreateUser(const Procs: USTRM_PROCS; const User: pointer): HLSTREAM; stdcall;
function lsStreamCreateMemory(const Size: cardinal): HLSTREAM; stdcall;
function lsStreamCreateMemoryLegacy(const Ptr: pointer; const Offset,Size: cardinal): HLSTREAM; stdcall;
function lsStreamCreateResource(Instance: THandle; Name, ResType: PChar; out Stream: HLSTREAM): HResult; stdcall;
function lsStreamQueryInterface(const lsStream: HLSTREAM; const IID: TGUID; out Obj): HResult; stdcall;

function lsStreamFree(var lsStream: HLSTREAM): HResult; stdcall;

function lsStreamIOCaps(const lsStream: HLSTREAM; out Caps: longint): HResult; stdcall;
function lsStreamSeek(const lsStream: HLSTREAM; const Offset: int64; const Origin: longint; out NewPosition: int64): HResult; stdcall;
function lsStreamGetSize(const lsStream: HLSTREAM; out StreamSize: int64): HResult; stdcall;
function lsStreamSetSize(const lsStream: HLSTREAM; const StreamSize: int64): HResult; stdcall;
function lsStreamRead(const lsStream: HLSTREAM; const Buffer; const BufferSz: longword; BytesRead: Plongint): HResult; stdcall;
function lsStreamWrite(const lsStream: HLSTREAM; const Buffer; const BufferSz: longword; BytesWritten: Plongint): HResult; stdcall;

implementation

const
  STRM_LIB = 'libstrms32.dll';

function lsStreamVersion; stdcall; external STRM_LIB;

function lsStreamCreateFile; stdcall; external STRM_LIB;
function lsStreamOpenFile; stdcall; external STRM_LIB;
function lsStreamCreateUser; stdcall; external STRM_LIB;
function lsStreamCreateMemory; stdcall; external STRM_LIB;
function lsStreamCreateMemoryLegacy; stdcall; external STRM_LIB;
function lsStreamCreateResource; stdcall; external STRM_LIB;
function lsStreamQueryInterface; stdcall; external STRM_LIB;

function lsStreamFree; stdcall; external STRM_LIB;

function lsStreamIOCaps; stdcall; external STRM_LIB;
function lsStreamSeek; stdcall; external STRM_LIB;
function lsStreamGetSize; stdcall; external STRM_LIB;
function lsStreamSetSize; stdcall; external STRM_LIB;
function lsStreamRead; stdcall; external STRM_LIB;
function lsStreamWrite; stdcall; external STRM_LIB;

end.
